import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import { Diagnostic as ParsingDiagnostic } from "./diagnostic";
import { diagnosticFlagToDiagnosticSeverity } from "./translation";

function createDiagnostic(textDocument: TextDocument, error: ParsingDiagnostic, type?: DiagnosticSeverity): Diagnostic {
    return {
        severity: type ?? DiagnosticSeverity.Error,
        range: {
            start: textDocument.positionAt(error.start),
            end: textDocument.positionAt(error.start + error.length)
        },
        message: error.message,
        source: "ds",
        code: error.code,
    };
}

export async function reportDiagnostics(diagnostics: ParsingDiagnostic[], textDocument: TextDocument) {
    const diags: Diagnostic[] = [];
    for (const d of diagnostics) {
        diags.push(createDiagnostic(textDocument, d, diagnosticFlagToDiagnosticSeverity(d.flag)));
    }
    return diags;
}
